Android 업데이트,다운로드,설치 기능 의 실현 을 검사 합 니 다.
선언:
우 맹 업데이트 가 곧 오프라인 되 기 때문에 우 리 는 업데이트 논 리 를 수정 하여 업데이트,다운로드,설 치 를 스스로 검 사 했 습 니 다.그러나 업 데 이 트 를 검사 하려 면 우 맹 의 온라인 매개 변수 에 의존 해 야 합 니 다.
1.MainActivity.Java:
public class MainActivity extends BaseActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckUpdateUtil.checkUpdate(this);//
}
}
2.CheckUpdateUtil.java:
package com.monkey.monkeymushroom.util;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.umeng.analytics.MobclickAgent;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
*/
public class CheckUpdateUtil {
private static NotificationCompat.Builder builder;
private static NotificationManager manager;
private static final int UPDATE_ID = "0";
/**
*
*
* @param context
* @return
*/
public static boolean checkUpdate(Context context) {
// ( )
String force_version = MobclickAgent.getConfigParams(context, "version");
if (StringUtils.isEmpty(version)) {
version = "1.0";
}
//
String[] mUpdateVersionArray = version.split(",");
String curr_version_name = SysInfoUtils.getVersionName(context);
for (int i = 0; i < mUpdateVersionArray .length; i++) {//
if (curr_version_name.equals(mUpdateVersionArray [i])) {// ,
if ((mUpdateVersionArray .length > i + 1) && ("Y".equals(mUpdateVersionArray [i + 1]))) {//
showUpdateDialog(true, context);
} else {//
showUpdateDialog(false, context);
}
return true;// , ,
}
}
return false;//
}
/**
*
*
* @param isForceUpdate
*/
public static void showUpdateDialog(final boolean isForceUpdate, final Context context) {
//
String update_log = MobclickAgent.getConfigParams(context, "update_log");
//
String new_version = MobclickAgent.getConfigParams(context, "new_version");
//
final String download_path = MobclickAgent.getConfigParams(context, "new_version_path");
if (TextUtils.isEmpty(update_log) || TextUtils.isEmpty(download_path) || TextUtils.isEmpty(new_version)) {
return;
}
LogMessage.e("monkey", " --> " + update_log + " --> " + new_version + " --> " + download_path);
//
final AlertDialog mAlertDialog = new AlertDialog.Builder(context).create();
mAlertDialog.show();
mAlertDialog.setCancelable(false);
Window window = mAlertDialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
View view = View.inflate(context, R.layout.dialog_update, null);
window.setContentView(view);
TextView log_head = (TextView) view.findViewById(R.id.log_head);
TextView msg_tv = (TextView) view.findViewById(R.id.msg_tv);
log_head.setText("v" + new_version + " :");
msg_tv.setText(update_log);
Button update = (Button) view.findViewById(R.id.yes_btn);
Button notNow = (Button) view.findViewById(R.id.no_btn);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
download(context, download_path);
mAlertDialog.dismiss();
}
});
notNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAlertDialog.dismiss();
}
});
if (isForceUpdate) {// , “ ”
notNow.setVisibility(View.GONE);
}
}
/**
* apk
*/
private static void download(final Context context, String download_path) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String apkName = download_path.substring(download_path.lastIndexOf("/") + 1);
String target = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + apkName;
LogMessage.e("monkey", "apk target -->" + target);
if (NetUtils.isNetConnect(context)) {
HttpUtils httpUtils = new HttpUtils(1000 * 10);// xUtils
httpUtils.download(download_path, target, false, true, new RequestCallBack<File>() {
@Override
public void onStart() {
super.onStart();
ToastUtil.show(context, " ……");
//
builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle(" ");
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
LogMessage.e("monkey", "--> total " + total + " current " + current);
int cur = (int) (current * 100 / total);
LogMessage.e("monkey", "cur--> " + cur + "%");
builder.setProgress(100, cur, false)//
.setContentText(cur + "%");
manager.notify(UPDATE_ID, builder.build());
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
manager.cancel(UPDATE_ID);//
// apk
File file = responseInfo.result;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
@Override
public void onFailure(HttpException e, String s) {
ToastUtil.show(context, " , ");
}
});
} else {
ToastUtil.show(context, " , ");
}
} else {
ToastUtil.show(context, "SD ");
}
}
}
3.탄 상자 레이아웃 파일 업데이트 dialogupdate.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/base_white"
android:paddingBottom="20dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text=" "
android:textSize="20sp" />
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/title"
android:background="#4a7acd" />
<TextView
android:id="@+id/log_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/line"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/msg_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/log_head"
android:layout_centerHorizontal="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text=" "
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_below="@+id/msg_tv"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<Button
android:id="@+id/yes_btn"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="#4a7acd"
android:gravity="center"
android:text=" "
android:textColor="@color/base_white"
android:textSize="16sp" />
<Button
android:id="@+id/no_btn"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="#4a7acd"
android:gravity="center"
android:text=" "
android:textColor="@color/base_white"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
탄 상자 업데이트:읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.